package com.glink.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.LogUtil;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.common.CommonUtils;
import com.glink.manage.dto.user.*;
import com.glink.manage.service.UserService;
import com.glink.manage.vo.user.UserBaseInfoVO;
import com.glink.manage.vo.user.UserPageVO;
import com.glink.manage.vo.user.UserSimpleInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : qiushaoshan
 * @create 2024/6/17 17:28
 */
@Slf4j
@RestController
@RequestMapping("/API/Manage/user")
@Api(tags = "用户管理")
public class UserController extends AbstractBaseController{

    @Autowired
    private UserService userService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "01.新增用户", notes = "1.新增用户")
    @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的用户id", dataType = "string", required = true)
    @PostMapping(value = "/insertUser", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> insertUser(@RequestBody @Validated UserSaveDTO userSaveDTO) throws GeneralSimpleException {
        log.info("01.新增用户：{}", LogUtil.encode(userSaveDTO));
        boolean status = userService.insertUser(userSaveDTO);
        log.info("01.新增用户：success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "02.修改用户", notes = "2.修改用户")
    @PostMapping(value = "/updateUser", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> updateUser(@RequestBody @Validated UserUpdateDTO updateDTO) throws GeneralSimpleException {
        log.info("02.修改用户：{}", LogUtil.encode(updateDTO));
        boolean status = userService.updateUser(updateDTO);
        log.info("02.修改用户：success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "03.用户禁用与启用", notes = "3.用户禁用与启用")
    @PostMapping(value = "/updateStatus", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> updateStatus(@RequestBody @Validated UserStatusDTO saveDTO) throws GeneralSimpleException {
        log.info("03.用户禁用与启用：{}", LogUtil.encode(saveDTO));
        boolean status = userService.updateStatus(saveDTO);
        log.info("03.用户禁用与启用：success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "04.用户信息分页查询", notes = "4.用户信息分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/findUserPage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<UserPageVO>> findUserPage(@RequestBody @Validated UserPageDTO pageDTO) throws GeneralSimpleException {
        log.info("04.用户信息分页查询：{}", LogUtil.encode(pageDTO));
        IPage<UserPageVO> userPageVOList = userService.findUserPage(pageDTO);
        log.info("04.用户信息分页查询：success");
        return this.response(userPageVOList);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "05.用户关联第三方账号", notes = "5.用户关联第三方账号")
    @PostMapping(value = "/relaThirdParty", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> relaThirdParty(@RequestBody @Validated UserRelaAccountDTO relaAccountDTO) throws GeneralSimpleException {
        log.info("05.用户关联第三方账号：{}", LogUtil.encode(relaAccountDTO));
        Boolean status = userService.relaThirdParty(relaAccountDTO);
        log.info("05.用户关联第三方账号：success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "06.根据用户ID获取用户信息", notes = "6.根据用户ID获取用户信息")
    @PostMapping(value = "/findById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<UserBaseInfoVO> findById(@RequestBody @Validated UserIdDTO userIdDTO) throws GeneralSimpleException {
        log.info("06.根据用户ID获取用户信息：{}", LogUtil.encode(userIdDTO));
        UserBaseInfoVO userBaseInfoVO = userService.findById(userIdDTO.getId());
        log.info("06.根据用户ID获取用户信息：success");
        return this.response(userBaseInfoVO);
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "07.获取当前用户信息", notes = "7.获取当前用户信息")
    @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的用户id", dataType = "string", required = true)
    @GetMapping(value = "/currentUser", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<UserSimpleInfoVO> findById() throws GeneralSimpleException {
        log.info("07.获取当前用户信息");
        String userId = CommonUtils.extractUserIdtByToken();
        UserSimpleInfoVO userBaseInfoVO = userService.currentUser(userId);
        log.info("07.获取当前用户信息：success");
        return this.response(userBaseInfoVO);
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "08.根据用户ID删除用户信息", notes = "08.根据用户ID删除用户信息")
    @PostMapping(value = "/deleteUser", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Object> deleteUser(@RequestBody @Validated UserIdDTO userIdDTO) throws GeneralSimpleException {
        log.info("08.根据用户ID删除用户信息,{}", LogUtil.encode(userIdDTO));
        Boolean aBoolean = userService.deleteUser(userIdDTO);
        log.info("08.根据用户ID删除用户信息：success");
        return this.response(aBoolean);
    }
}
